package com.riber.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.riber.student.common.ResponseResult;
import com.riber.student.dao.RcCourseMapper;
import com.riber.student.entity.RcStudent;
import com.riber.student.dao.RcStudentMapper;
import com.riber.student.service.IRcStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.riber.student.vo.input.SearchChoseCourseVo;
import com.riber.student.vo.output.ChoseCourseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author riber
 * @since 2022-10-25
 */
@Service
public class RcStudentServiceImpl extends ServiceImpl<RcStudentMapper, RcStudent> implements IRcStudentService {
    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcCourseMapper courseMapper;


    @Override
    public ResponseResult updateStudent(RcStudent stu) {
        RcStudent rcStudent = studentMapper.selectById(stu.getStudentId());
        if (rcStudent == null) {
            return ResponseResult.createFail("该学生不存在,请重试或联系管理员");
        }
        if (rcStudent.getStudentBirthday() != null && rcStudent.getStudentBirthday().getTime() > System.currentTimeMillis()) {
            return ResponseResult.createFail("该学生出生日期不匹配");
        }
        // 复制 stu的数据给 rcStudent
        BeanUtils.copyProperties(stu, rcStudent);
        if (studentMapper.updateById(rcStudent) > 0) {
            return ResponseResult.createSuccess("修改成功");
        }
        return ResponseResult.createFail("修改失败");
    }

    @Override
    public ResponseResult getStudentInfo(Integer userId) {
        return ResponseResult.createSuccess(studentMapper.getStudentInfo(userId));
    }

    @Override
    public ResponseResult searchCourse(SearchChoseCourseVo courseVo) {
        PageHelper.startPage(courseVo.getIndex(), 8);
        List<ChoseCourseInfoVo> list = courseMapper.searchCourse(courseVo);
        PageInfo<ChoseCourseInfoVo> info = new PageInfo<>(list);
        // 查询 年级
        // 查询 院系id
        // 查询 列表 用户已选择的课程id list
        return ResponseResult.createSuccess(info);
    }


}
